#include "TestScene1.h"

float rotation = 0;
float scalation = 0;
float translation = 0;
short coloration = 0;

float translationX = -200;
float translationY = 0;

const std::string WALK_RIGHT = "WalkRight";
const std::string WALK_LEFT = "WalkLeft";

/*-----------------------------------------------------------------------------------------------*/
TestScene1::TestScene1(std::string name) : Scene(name)
{
   m_pCamera = new Camera();
}
/*-----------------------------------------------------------------------------------------------*/
TestScene1::~TestScene1()
{
 //  delete m_pTriangle;
 //  delete m_pSquare;
   delete m_pAirplane;
   delete m_pTank;
   delete m_pCamera;
}
/*-----------------------------------------------------------------------------------------------*/
bool TestScene1::onInit(Renderer* renderer,Importer* pImporter)
{
   ////////////////////////////// <Meshes> ///////////////////////////////
   //m_pCube = new Cube("cubo");
   //addEntity(m_pCube);
   //m_pCube->Scale(2,2,2);
   //m_pCube->setMaterial();

   //m_pTestCube = new TestCube("testcube");
   //addEntity(m_pTestCube);
   //m_pTestCube->Scale(30,30,30);

   if ( !pImporter->importModel("./Resources/Airplane/airplane 2.x") )
   {
      std::cout << "Could not import airplane" << std::endl;
   }

   if ( !pImporter->importModel("./Resources/Tank/tank.x") )
   {
      std::cout << "Could not import tank" << std::endl;
   }

   m_pAirplane = pImporter->getModel("airplane 2.x");
   
   m_pTank = pImporter->getModel("./Resources/Tank/tank.x");
   
   if (m_pAirplane != NULL)
   {
      addEntity(m_pAirplane);
   }

   if (m_pTank != NULL)
   {
      addEntity(m_pTank);
   }
   

   m_pAirplane->Scale(10,10,10);
   
   m_pAirplane->Translate(100);

   ////////////////////////////// <Shapes> ///////////////////////////////

   //m_pTriangle = new Triangle("triangulo");
   //m_pSquare = new Square("cuadrado");  

   //addEntity(m_pTriangle);
   //addEntity(m_pSquare);


   //m_pTriangle->Scale(2,2);
   //m_pTriangle->setVisible(false);
   
   //m_pSquare->Scale(80,80);
   //m_pSquare->setVisible(false);

   //m_pSquare->setColor(0,0,255);

   ////////////////////////////// <Sprites> ///////////////////////////////

//    if ( !pImporter->importResources("./Resources/Sprites.xml") )
//    {
//       std::cout << "Could not import resources" << std::endl;
//       return false;
//    }
//    
//    m_pWonderboy = new Sprite("wonderboy");
// 
//    if ( !pImporter->createSprite(m_pWonderboy->getName().c_str() , m_pWonderboy) )
//    {
//       std::cout << "Failed to create sprite: " << m_pWonderboy->getName() << std::endl;
//       return false;
//    }
//    
//    m_pWonderboy->Scale(100,100);
//    m_pWonderboy->Translate(translationX,0,0);
// 
//    if ( !m_pWonderboy->setAnimation(WALK_LEFT) )
//    {
//       std::cout << "Failed to set animation " << WALK_LEFT << std::endl;  
//       return false;
//    }
// 
//    m_pWonderboy->getAnimation()->stop();
// 
//    m_pWonderboy->setMooving(true);

//    if (!addEntity(m_pWonderboy))
//    {
//       std::cout << "Failed to add entity: " << m_pWonderboy->getName() << std::endl;
//    }

   //////////////////////////////////////////////////////////////////////////
   //collisions
   //////////////////////////////////////////////////////////////////////////


//   addToCollisionGrup(std::string("1"),m_pWonderboy);
//   addToCollisionGrup(std::string("2"),m_pSquare);

   //////////////////////////////////////////////////////////////////////////
   //Lights
   //////////////////////////////////////////////////////////////////////////
   m_pDirectionalLight = new DirectionalLight(Vector3(-1,-1,1));

   m_pDirectionalLight->setLight(renderer);

   return true;
}
/*-----------------------------------------------------------------------------------------------*/
void TestScene1::onUpdate()
{
   ////////////////////////////// <Shapes> ///////////////////////////////
   translation += (translation < 500) ? 0.1f : -1000.0f;
   rotation += 1.1f;
   scalation += (scalation < 500) ? 0.05f : -500.0f;
   coloration += (scalation < 256) ? 1 : -255;

   //m_pSquare->setColor(0,0,coloration);
   //m_pSquare->Translate(translation);
   //m_pSquare->Rotate(0,0,rotation);
   //m_pSquare->Scale(scalation,scalation);

   //m_pCube->Rotate(rotation,rotation,rotation);
   //m_pTestCube->Rotate(rotation,rotation,rotation);
   //m_pAirplane->Rotate(rotation,rotation,rotation);

   if(getInput()->getKeyDown(DIK_W))
   {
      m_pCamera->Walk(0.1f);
   }

   if(getInput()->getKeyDown(DIK_S))
   {
      m_pCamera->Walk(-0.1f);
   }

   if(getInput()->getKeyDown(DIK_A))
   {
      m_pCamera->Strafe(-0.1f);
   }

   if(getInput()->getKeyDown(DIK_D))
   {
      m_pCamera->Strafe(0.1f);
   }

   if(getInput()->getKeyDown(DIK_UP))
   {
      m_pCamera->Pitch(-0.001f);
   }
   if(getInput()->getKeyDown(DIK_DOWN))
   {
      m_pCamera->Pitch(0.001f);
   }
   if(getInput()->getKeyDown(DIK_LEFT))
   {
      m_pCamera->Yaw(-0.001f);
   }
   if(getInput()->getKeyDown(DIK_RIGHT))
   {
      m_pCamera->Yaw(0.001f);
   }

   Node* torreta = m_pTank->getNodeByName("Torreta"); 
   if (torreta)
   {
      torreta->Rotate(0,rotation);
   }
   

}
/*-----------------------------------------------------------------------------------------------*/
void TestScene1::onDraw(Renderer* renderer)
{
   m_pCamera->Draw(renderer);
}